<template>
  <view class="order-serve common-container">
    <wd-form
      custom-class="common-form"
      ref="formRef"
      :model="formModel"
    >
      <wd-cell-group>
        <wd-input
          label="点单服务名称"
          label-width="90px"
          prop="name"
          v-model="formModel.name"
          placeholder="请输入"
        />
        <wd-picker
          :columns="hallColumns"
          label="所属礼堂"
          v-model="formModel.hallId"
        />
        <wd-datetime-picker
          label="活动时间"
          type="date"
          :min-date="nowDate"
          v-model="formModel.date"
        />
        <wd-input
          label="活动地点"
          label-width="90px"
          prop="address"
          v-model="formModel.address"
          placeholder="请输入"
        />
        <wd-cell title="活动介绍">
          <wd-textarea
            v-model="formModel.note"
            placeholder="请输入"
          />
        </wd-cell>
      </wd-cell-group>
    </wd-form>
    <wd-button
      type="primary"
      @click="submitForm"
      :round="false"
      block
    >
      提交申请
    </wd-button>
  </view>
</template>

<script setup>
import HallApi from '@/service/hall'
import OrderApi from '@/service/order'
import { useDictStore } from '@/store/dict'
import { prevPageNeedRefresh } from '@/utils'
import dayjs from 'dayjs'

const { dictData } = useDictStore()

const nowDate = new Date().getTime()

const initFormModel = {
  name: '',
  hallId: '',
  date: nowDate,
  address: '',
  note: '',
}

const formRef = ref()
const formModel = ref({
  name: '',
  hallId: '',
  date: nowDate,
  address: '',
  note: '',
})
const formKeyMap = {
  name: '点单服务名称',
  hallId: '所属礼堂',
  date: '活动时间',
  address: '活动地点',
  note: '活动介绍',
}

const hallColumns = ref([])
const getHallDict = async () => {
  const { code, result } = await HallApi.getHallDict()
  if (code === 200 && result) {
    hallColumns.value = result
  }
}
getHallDict()

const submitForm = () => {
  let objKey = ''
  for (const key in formKeyMap) {
    if (!formModel.value[key]) {
      objKey = key
      break
    }
  }
  if (objKey) {
    uni.showToast({
      title: `${formKeyMap[objKey]}不能为空`,
      icon: 'none',
    })
    return false
  }

  formRef.value
    .validate()
    .then(async () => {
      const date = dayjs(formModel.value.date).format('YYYY-MM-DD')
      const params = {
        ...formModel.value,
        date,
      }
      const { code, result, message } =
        await OrderApi.addOrderApplication(params)
      if (code === 200 && result) {
        uni.showToast({
          icon: 'success',
          title: '添加成功',
        })
        formModel.value = initFormModel
        setTimeout(() => {
          isNavigateBack.value = true
          uni.navigateBack()
        }, 1000)
      } else {
        uni.showToast({
          icon: 'error',
          title: message,
        })
      }
    })
    .catch((error) => {
      console.error(error)
    })
}

const isNavigateBack = ref(false)
onUnload(() => {
  prevPageNeedRefresh(isNavigateBack.value)
})
</script>

<style lang="scss">
@import '../../../../style/common-form';

.order-serve {
  height: calc(100vh - 46px);
  overflow: scroll;
}
</style>
